﻿// <copyright file="SketchFlowAnalytics.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-11-30</date>
// <summary>SketchFlowAnalytics class definition</summary>

namespace Microsoft.WebAnalytics.SketchFlow
{
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.ComponentModel.Composition;
    using System.Linq;
    using System.Windows;
    using Microsoft.Expression.Prototyping.Annotation;
    using Microsoft.Expression.Prototyping.Navigation;
    using Microsoft.Expression.Prototyping.Workspace;
    using Microsoft.WebAnalytics;

    /// <summary>
    /// Use this to create the LayoutRoot for a SketchFlow application
    /// You should also add the WebAnalyticsService and service behaviors to the Application.ApplicationLifetimeObjects
    /// </summary>
    public class SketchFlowAnalytics
    {
        /// <summary>
        /// the player window
        /// </summary>
        private PlayerWindow playerWindow;

        /// <summary>
        /// Gets or sets the Log method
        /// </summary>
        [Import("Log")]
        public Action<AnalyticsEvent> Log { get; set; }

        /// <summary>
        /// Creates the RootVisual element in the Application_Startup event handler
        /// </summary>
        /// <param name="application">the new application instance in the Application_Startup event handler</param>
        /// <param name="playerWindow">a new SketchFlow Player Window</param>
        /// <returns>a Grid element that contains the PlayerWindow and the analytics behaviors</returns>
        public UIElement CreateRootVisual(Application application, PlayerWindow playerWindow)
        {
            this.playerWindow = playerWindow;

            PartInitializer.SatisfyImports(this);

            this.playerWindow.NavigationViewModel.CurrentScreenChanged += new EventHandler<ScreenChangedEventArgs>(this.OnCurrentScreenChanged);

            this.playerWindow.AnnotationViewModel.TextFeedbackEntries.CollectionChanged += new NotifyCollectionChangedEventHandler(this.TextFeedbackEntries_CollectionChanged);

            return this.playerWindow;
        }

        /// <summary>
        /// Text Feedback entries collection changed event handler
        /// </summary>
        /// <param name="sender">the observable collection of FeedbackTextItemEntry objects</param>
        /// <param name="e">the notification collection changed event arguments</param>
        private void TextFeedbackEntries_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    this.LogFeedbackEntryAdded(e.NewItems.Cast<FeedbackTextItemEntry>());
                    break;
            }
        }

        /// <summary>
        /// Current Screen changed event handler
        /// </summary>
        /// <param name="sender">the NavigationViewModel</param>
        /// <param name="e">the Screen changed event arguments</param>
        private void OnCurrentScreenChanged(object sender, ScreenChangedEventArgs e)
        {
            var navViewModel = sender as NavigationViewModel;

            var analtyicsEvent = new AnalyticsEvent()
            {
                ActionValue = e.CurrentScreen,
                HitType = Microsoft.WebAnalytics.Data.HitType.PageView,
                Name = "CurrentScreenChanged",
                ObjectName = navViewModel.CurrentScreenDescription,
                ObjectType = sender.GetType().Name
            };

            this.Log(analtyicsEvent);
        }

        /// <summary>
        /// Log when a Feedback entry is added
        /// </summary>
        /// <param name="newItems">the new feedback text items entries</param>
        private void LogFeedbackEntryAdded(IEnumerable<FeedbackTextItemEntry> newItems)
        {
            var events = from entry in newItems
                         select new AnalyticsEvent()
                         {
                             ActionValue = entry.Entry,
                             Name = "FeedbackTextAdded",
                             ObjectName = this.playerWindow.NavigationViewModel.CurrentScreen
                         };

            foreach (var logEvent in events)
            {
                this.Log(logEvent);
            }
        }
    }
}
